package cn.nju.zyy.action.person;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.springframework.stereotype.Component;

import cn.nju.zyy.model.person.Person;
import cn.nju.zyy.model.person.State;

import com.opensymphony.xwork2.ActionSupport;

@Component
public class RegisterAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	/*
	 * The framework will create an instance of Person and call its public set
	 * methods before calling the execute method.
	 */
	private Person person;
	private String[] sports = { "football", "baseball", "basketball" };
	private String[] genders = { "male", "female", "not sure" };
	private List<State> states;
	private String[] carModelsAvailable = { "Ford", "Chrysler", "Toyota",
			"Nissan" };
	
	@Override
	@Action("register-input")
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String execute() throws Exception {
		// call Service class to store person's state in database
		return SUCCESS;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public String[] getSports() {
		return sports;
	}

	public String[] getGenders() {
		return genders;
	}

	public List<State> getStates() {
		states = new ArrayList<State>();
		states.add(new State("AZ", "Arizona"));
		states.add(new State("CA", "California"));
		states.add(new State("FL", "Florida"));
		states.add(new State("KS", "Kansas"));
		states.add(new State("NY", "New York"));
		return states;
	}

	public String[] getCarModelsAvailable() {
		return carModelsAvailable;
	}

	/*
	 * If any errors have been added then Struts 2 will not proceed to call the
	 * execute method. Rather the Struts 2 framework will return "input" as the
	 * result of calling the action.
	 */
	@Override
	public void validate() {
		if (person.getFirstName().length() == 0) {
			addFieldError("person.firstName", "First name is required.");
		}
		if (person.getEmail().length() == 0) {
			addFieldError("person.email", "Email is required.");
		}
	}

}
